/*
  Copyright 2011 Frogtek BOP LLC

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package org.frogtek.database;

import java.util.Map;

/**
 * 
 * 
 * @author Jose B. Cortés (jose@frogtek.org)
 * @creationDate 5/04/2010
 *
 */

public class DatabaseGenerals {
	
	
	/**
	 * Creates a <code>CREATE TABLE</code> script using a metadata Map and returns it as String
	 *
	 * @param sTableName name of table whose <code>CREATE TABLE</code> script has to be created
	 * @param oMap map defining the meta-data of the DB table used to obtain the appropriate server name corresponding to each ContentValue
	 * @return a String containing the <code>CREATE TABLE</code> script
	 */
	public static String getCreateTableScript(String sTableName, Map<String, String[]> oMap) {
		
	    StringBuilder scriptBuilder = new StringBuilder();
	    scriptBuilder.append("CREATE TABLE IF NOT EXISTS " + sTableName + " (");

		for (Map.Entry<String, String[]> entry : oMap.entrySet()) {
			String[] dbProperties = entry.getValue();
			scriptBuilder.append(entry.getKey() + " " + dbProperties[1] + " " + dbProperties[2] + ", ");
		}

		// remove last ", " and add close script
		String sCreationScript = scriptBuilder.toString().substring(0,  scriptBuilder.toString().length() - 2) + " );";
		return sCreationScript;
	}

}
